//
//  StringUtils.java
//  doc-recode
//
//  Created by Eden He on 2013-7-3
//  Copyright (c) 2013 Chunyu.mobi 
//  All rights reserved
//

package com.aj.utils;

import android.text.TextUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    /**
     * 判断字符串是否包含空白字符
     *
     * @param content
     * @return
     */
    public static boolean isContainWhitespaceCharacter(String content) {
        if (content == null) {
            return false;
        }
        Pattern pattern = Pattern.compile("\\s");
        Matcher matcher = pattern.matcher(content);
        return matcher.find();
    }

    /**
     * 转化字符串到MD5格式
     *
     * @param value
     * @return
     */
    public static String convertToMD5Format(String value) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(value.getBytes());
            byte messageDigest[] = digest.digest();
            StringBuilder hexString = new StringBuilder();
            for (byte element : messageDigest) {
                hexString.append(String.format("%02x", 0xFF & element));
            }
            return hexString.toString();
        } catch (Exception e) {
            return value;
        }
    }

    /**
     * 字符串是否相等 null=""
     *
     * @param str1 , str2
     * @return
     */
    public static boolean isEqual(String str1, String str2) {
        boolean isStr1Empty = TextUtils.isEmpty(str1);
        boolean isStr2Empty = TextUtils.isEmpty(str2);

        if (!isStr1Empty && !isStr2Empty) {
            return str1.equals(str2);
        } else {
            return !(isStr1Empty ^ isStr2Empty);
        }
    }

    /**
     * 将手机号规范化，只最后11位数字
     *
     * @param phone
     * @return
     */
    public static String normalizePhoneNumber(String phone) {
        Pattern p = Pattern.compile("[^0-9]");
        Matcher m = p.matcher(phone);
        phone = m.replaceAll("");
        if (phone.length() > 11) {
            phone = phone.substring(phone.length() - 11);
        }
        return phone;
    }

    public static boolean isCNChar(char c) {
        return (c >= 0x4e00 && c <= 0x9fa5);
    }

    public static boolean isENChar(char c) {
        if (c >= 'a' && c <= 'z')
            return true;
        if (c >= 'A' && c <= 'Z')
            return true;
        return false;
    }

    public static boolean isNumChar(char c) {
        return (c >= '0' && c <= '9');
    }
    
    public static String md5(String str) {
        MessageDigest md;
        String dstr = null;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            dstr = new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dstr;
    }
    
    public static boolean isStringEmpty(String string){
    	if(string==null){
    		return true;
    	}else{
    		if(string.trim().length()==0){
    			return true;
    		}else{
    			return false;
    		}
    	}
    }
}
